/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package MeteoCal.controller;

import MeteoCal.bean.exceptions.NonexistentEntityException;
import MeteoCal.bean.exceptions.PreexistingEntityException;
import MeteoCal.bean.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import MeteoCal.entity.Comment;
import MeteoCal.entity.User;
import MeteoCal.entity.Warningcomment;
import MeteoCal.entity.WarningcommentPK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;

/**
 *
 * @author Daniele
 */
public class WarningcommentJpaController implements Serializable {

    public WarningcommentJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Warningcomment warningcomment) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (warningcomment.getWarningcommentPK() == null) {
            warningcomment.setWarningcommentPK(new WarningcommentPK());
        }
        warningcomment.getWarningcommentPK().setIdComment(warningcomment.getComment().getIdComment());
        warningcomment.getWarningcommentPK().setIdUser(warningcomment.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Comment comment = warningcomment.getComment();
            if (comment != null) {
                comment = em.getReference(comment.getClass(), comment.getIdComment());
                warningcomment.setComment(comment);
            }
            User user = warningcomment.getUser();
            if (user != null) {
                user = em.getReference(user.getClass(), user.getEmail());
                warningcomment.setUser(user);
            }
            em.persist(warningcomment);
            if (comment != null) {
                comment.getWarningcommentCollection().add(warningcomment);
                comment = em.merge(comment);
            }
            if (user != null) {
                user.getWarningcommentCollection().add(warningcomment);
                user = em.merge(user);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findWarningcomment(warningcomment.getWarningcommentPK()) != null) {
                throw new PreexistingEntityException("Warningcomment " + warningcomment + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Warningcomment warningcomment) throws NonexistentEntityException, RollbackFailureException, Exception {
        warningcomment.getWarningcommentPK().setIdComment(warningcomment.getComment().getIdComment());
        warningcomment.getWarningcommentPK().setIdUser(warningcomment.getUser().getEmail());
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Warningcomment persistentWarningcomment = em.find(Warningcomment.class, warningcomment.getWarningcommentPK());
            Comment commentOld = persistentWarningcomment.getComment();
            Comment commentNew = warningcomment.getComment();
            User userOld = persistentWarningcomment.getUser();
            User userNew = warningcomment.getUser();
            if (commentNew != null) {
                commentNew = em.getReference(commentNew.getClass(), commentNew.getIdComment());
                warningcomment.setComment(commentNew);
            }
            if (userNew != null) {
                userNew = em.getReference(userNew.getClass(), userNew.getEmail());
                warningcomment.setUser(userNew);
            }
            warningcomment = em.merge(warningcomment);
            if (commentOld != null && !commentOld.equals(commentNew)) {
                commentOld.getWarningcommentCollection().remove(warningcomment);
                commentOld = em.merge(commentOld);
            }
            if (commentNew != null && !commentNew.equals(commentOld)) {
                commentNew.getWarningcommentCollection().add(warningcomment);
                commentNew = em.merge(commentNew);
            }
            if (userOld != null && !userOld.equals(userNew)) {
                userOld.getWarningcommentCollection().remove(warningcomment);
                userOld = em.merge(userOld);
            }
            if (userNew != null && !userNew.equals(userOld)) {
                userNew.getWarningcommentCollection().add(warningcomment);
                userNew = em.merge(userNew);
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                WarningcommentPK id = warningcomment.getWarningcommentPK();
                if (findWarningcomment(id) == null) {
                    throw new NonexistentEntityException("The warningcomment with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(WarningcommentPK id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Warningcomment warningcomment;
            try {
                warningcomment = em.getReference(Warningcomment.class, id);
                warningcomment.getWarningcommentPK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The warningcomment with id " + id + " no longer exists.", enfe);
            }
            Comment comment = warningcomment.getComment();
            if (comment != null) {
                comment.getWarningcommentCollection().remove(warningcomment);
                comment = em.merge(comment);
            }
            User user = warningcomment.getUser();
            if (user != null) {
                user.getWarningcommentCollection().remove(warningcomment);
                user = em.merge(user);
            }
            em.remove(warningcomment);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Warningcomment> findWarningcommentEntities() {
        return findWarningcommentEntities(true, -1, -1);
    }

    public List<Warningcomment> findWarningcommentEntities(int maxResults, int firstResult) {
        return findWarningcommentEntities(false, maxResults, firstResult);
    }

    private List<Warningcomment> findWarningcommentEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Warningcomment.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Warningcomment findWarningcomment(WarningcommentPK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Warningcomment.class, id);
        } finally {
            em.close();
        }
    }

    public int getWarningcommentCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Warningcomment> rt = cq.from(Warningcomment.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
